package online.yysh.charging.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;

import online.yysh.charging.R;
import online.yysh.charging.base.BaseFragment;
import online.yysh.charging.bean.OrderBean;
import online.yysh.charging.bean.response.GetOrderResponse;
import online.yysh.charging.utils.HttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 已完成订单
 */
public class CompleteFragment extends BaseFragment {

    private RecyclerView rv_complete;
    private List<OrderBean> completeOrders = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_complete, null);
        rv_complete = view.findViewById(R.id.rv_complete);
        return view;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 200:
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    private void getCompleteOrders() {
        HttpUtil.getInstance().getOrders(openid, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                showToast("查询已完成订单失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "getOrders: " + result);
                if (result.contains("ok")){
                    GetOrderResponse getOrderResponse = new Gson().fromJson(result, GetOrderResponse.class);
                    List<OrderBean> orders = getOrderResponse.getData();
                    completeOrders = orders;
                    handler.sendEmptyMessage(200);
                }else{
                    showToast("无已完成订单");
                }
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        rv_complete.setLayoutManager(new LinearLayoutManager(getActivity()));
        getCompleteOrders();
        adapter = new MyAdapter();
        rv_complete.setAdapter(adapter);
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        @NonNull
        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
            OrderBean orderBean = completeOrders.get(position);
            holder.tv_no.setText("订单号：" + orderBean.getOrder_no());
//            holder.tv_device_id.setText("设备ID："+orderBean.getBox_IMEI());
            String his_stopreson = orderBean.getStop_reson();
            switch (his_stopreson) {
                case "0":
                    his_stopreson = "未知原因停止";
                    break;
                case "1":
                    his_stopreson = "桩主动停止";
                    break;
                case "2":
                    his_stopreson = "平台主动停止";
                    break;
                case "3":
                    his_stopreson = "BMS主动终止充电";
                    break;
                case "4":
                    his_stopreson = "金额不足";
                    break;
                case "5":
                    his_stopreson = "预约电量到";
                    break;
                case "6":
                    his_stopreson = "预约时间到";
                    break;
                case "7":
                    his_stopreson = "预约金额到";
                    break;
                case "8":
                    his_stopreson = "急停按钮按下";
                    break;
                case "9":
                    his_stopreson = "防雷器状态异常";
                    break;
                case "10":
                    his_stopreson = "充电枪连接异常";
                    break;
                case "11":
                    his_stopreson = "BMS通讯中断";
                    break;
                case "12":
                    his_stopreson = "BMS辨识失败";
                    break;
                case "13":
                    his_stopreson = "电能表通讯异常";
                    break;
                case "14":
                    his_stopreson = "直流输出过压";
                    break;
                case "15":
                    his_stopreson = "直流输出过流";
                    break;
                case "16":
                    his_stopreson = "交流输入过压";
                    break;
                case "17":
                    his_stopreson = "交流输入欠压";
                    break;
                case "18":
                    his_stopreson = "电池电压不正常";
                    break;
                case "19":
                    his_stopreson = "充电枪反接";
                    break;
                case "20":
                    his_stopreson = "充电枪温度异常";
                    break;
                case "21":
                    his_stopreson = "绝缘检测故障";
                    break;
                case "22":
                    his_stopreson = "充电模块通讯异常";
                    break;
                case "23":
                    his_stopreson = "读卡器通讯异常";
                    break;
                case "24":
                    his_stopreson = "直流接触器状态异常";
                    break;
                default:
                    his_stopreson = "其它异常";
                    break;
            }

            holder.tv_device_id.setText("停止原因：" + his_stopreson);
            holder.tv_time.setText("起止时间：" + orderBean.getStart_time() + "-" + orderBean.getEnd_time());
            holder.tv_other_money.setText("服务费(元)：" + orderBean.getOther_money().substring(0, 4));
            holder.tv_quantity.setText("充电量(kwh)：" + orderBean.getUserKWh().substring(0, 4));
            holder.tv_money.setText("账户余额(元)：" + orderBean.getIc_money());
            holder.tv_use_money.setText("充电金额(元)：" + orderBean.getUse_money().substring(0, 4));
        }

        @Override
        public int getItemCount() {
            return completeOrders.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tv_no, tv_time, tv_device_id, tv_quantity, tv_use_money, tv_other_money, tv_money;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                tv_no = itemView.findViewById(R.id.tv_no);
                tv_time = itemView.findViewById(R.id.tv_time);
                tv_device_id = itemView.findViewById(R.id.tv_device_id);
                tv_quantity = itemView.findViewById(R.id.tv_quantity);
                tv_use_money = itemView.findViewById(R.id.tv_use_money);
                tv_other_money = itemView.findViewById(R.id.tv_other_money);
                tv_money = itemView.findViewById(R.id.tv_money);
            }
        }
    }
}
